<template>
  <div class="app-container">
    <div style="padding:10px 0">
      <el-form :inline="true" :model="listQuery" size="mini" label-width="100px">
          <el-form-item label="关键字：">
            <el-input v-model="listQuery.keywords" clearable placeholder="客户名称/加入原因"></el-input>
          </el-form-item>
          <el-form-item label="加入时间：">
            <el-date-picker
              v-model="listQuery.createTime"
              type="daterange"
              format="yyyy 年 MM 月 dd 日"
              value-format="yyyy-MM-dd"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button @click="handleSearchList" type="primary">查询</el-button>
            <el-button size="mini" type="primary" class="btn-add" @click="showDialog()" v-if="addCustomerBlackList">新增黑名单客户</el-button>
          </el-form-item>
      </el-form>

    </div>
    <el-table :data="data" style="width: 100%;font-size: 12px;" v-loading="dataLoading" border>
        <el-table-column label="客户名称"  align="center" min-width="200">
          <template slot-scope="scope">{{scope.row.customerName}}</template>
        </el-table-column>

        <el-table-column label="备注"  align="center" min-width="250">
          <template slot-scope="scope">{{scope.row.remark}}</template>
        </el-table-column>

        <el-table-column label="加入时间"  align="center" min-width="100">
          <template slot-scope="scope">
             {{scope.row.createTime}}
          </template>
        </el-table-column>
      <el-table-column label="负责人"  align="center" min-width="100">
        <template slot-scope="scope">{{scope.row.chargeName}}</template>
      </el-table-column>

      </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        layout="total, sizes,prev, pager, next,jumper"
        :current-page.sync="listQuery.pageNum"
        :page-size="listQuery.pageSize"
        :page-sizes="[10,15,20]"
        :total="count">
      </el-pagination>
    </div>
    <!--新增/编辑行业-->
    <el-dialog
      :close-on-click-modal="false"
      title="新增黑名单客户"
      :visible.sync="dialogVisible"
      width="420px">
      <el-form :model="form" size="mini" label-width="120px" :rules="rules" ref="form">
        <el-form-item label="客户名称：" prop="customerName">
          <el-input v-model="form.customerName" placeholder="需输入完整的名称"></el-input>
        </el-form-item>

        <el-form-item label="加入原因：" prop="remark">
          <el-input v-model="form.remark" :autosize="{ minRows: 2, maxRows: 6}" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleConfirm" :loading="btnLoading">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>

import {blacklist,addBlacklist} from '@/api/company'

import store from '@/store'
const defaultListQuery = {
  keywords:'',
  createTime:[],
  pageNum: 1,
  pageSize: 10,
};

const defaultForm = {
  customerName:'',
  remark:'',
};
export default {
  name: "blacklist",
  data(){
    return{
      data : [],
      dialogVisible:false,
      form:JSON.parse(JSON.stringify(defaultForm)),
      rules: {
        customerName:[{required: true, message: '此项必填', trigger: 'blur'},],
        remark:[{required: true, message: '此项必填', trigger: 'blur'},],
      },
      btnLoading:false,
      dataLoading:false,
      listQuery: JSON.parse(JSON.stringify(defaultListQuery)),
      count:0,
      roles:store.getters.roles ? store.getters.roles : [],

    }
  },
  created() {
    this.getList();

  },
  computed: {
    addCustomerBlackList(){
      if(this.roles.indexOf("addCustomerBlackList") != -1){
        return true
      }else{
        return false
      }
    },
  },

  filters: {

  },

  methods:{
    toInfo(item){
      this.$router.push({name:'customerDetail',query:{id:item.customerId}})
    },
    handleSizeChange(val){
      this.listQuery.pageNum = 1;
      this.listQuery.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val){
      this.listQuery.pageNum = val;
      this.getList();
    },

    handleSearchList(){
      this.listQuery.pageNum = 1;
      this.getList();
    },
    //获取列表
    getList(){
      this.dataLoading = true;
      blacklist(this.listQuery).then((res)=>{
        this.data = res.list;
        this.count = res.count
        this.dataLoading = false;
      })
    },

    //显示新增
    showDialog(){
      this.dialogVisible = true;
    },

    handleClose(){
      this.dialogVisible = false;
    },

    //新增
    handleConfirm(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.btnLoading = true;
          let data = JSON.parse(JSON.stringify(this.form));
          addBlacklist(data).then(response=>{
            this.dialogVisible = false;
            this.$message({
              message: '新增成功',
              type: 'success',
              duration:1000
            });
            this.btnLoading = false;
            this.getList()
          }).catch(()=>{
            this.btnLoading = false;
          })
        }
      });
    },

  }

}
</script>

